package org.ands.apps;

import java.util.HashMap;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.namespace.NamespaceContext;

import org.ands.rifcs.RifCsNamespaceContext;
import org.ands.xml.DOMHelper;
import org.ands.xml.DOMHelperConstruct;
import org.ands.xml.DOMHelperFromFile;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

public class SandBox {

	/**
	 * @param args
	 * @throws Exception 
	 * @throws ParserConfigurationException 
	 */
	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		
		try{
			DOMHelperFromFile domHelper = new DOMHelperFromFile("SandBoxReadExample.xml");
			assert(domHelper.docElement().hasChildNodes());
			System.out.println("\n\nPrint info for read...\n");
			printInfo(domHelper);
		}
		catch(Exception e){
			System.out.println("Exception: "+e.getMessage());
		}
			
			
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		dbf.setNamespaceAware(true);
		dbf.setCoalescing(true);
		dbf.setIgnoringElementContentWhitespace(true);
		dbf.setIgnoringComments(true);
		
		DocumentBuilder docBuilder = null;
		
		try{
			
			docBuilder = dbf.newDocumentBuilder();
		}
		catch(Exception e){
			System.out.println("Failed to construct document - exception...\n"+e.getMessage());
			System.exit(-1);
		}
		//DOMImplementation domImplementation = docBuilder.getDOMImplementation();
		
		String namespaceURI = "http://ands.org.au/standards/rif-cs/registryObjects";
		
		NamespaceContext namespaceContext = (NamespaceContext) new RifCsNamespaceContext();
		
		DOMHelperConstruct domHelper = new DOMHelperConstruct(namespaceContext, namespaceURI, "registryObjects", null);
			
		Element root = domHelper.docElement();
		//root.setPrefix("ro");
		//root.setAttribute("xmlns", namespaceURI);
		
				
		Map<String, String> docRootAttributeMap = new HashMap<String, String>();
		docRootAttributeMap.put("xmlns", namespaceURI);
		docRootAttributeMap.put("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
		docRootAttributeMap.put("xsi:schemaLocation", "http://ands.org.au/standards/rif-cs/registryObjects");
		
		try{
			
			domHelper.createXmlAsPerXpath(namespaceURI, "/registryObjects[1]/registryObject[1]", "");
			domHelper.createXmlAsPerXpath(namespaceURI, "/registryObjects[1]/registryObject[1]/@group", "Atlas of Living Australia");
			domHelper.createXmlAsPerXpath(namespaceURI, "/registryObjects[1]/registryObject[1]/key[1]", "keyvalue");
			
		}catch(Exception e){
			System.out.println("Failed to construct xpath element exception...\n"+e.getMessage());
			System.exit(-1);
		}
		
		try{
			
			System.out.println("\n\nPrint info for constructed...\n");
			printInfo(domHelper);
			
			domHelper.writeToFile("Sandbox.xml");
			
		}catch(Exception e){
			System.out.println("Failed - exception...\n"+e.getMessage());
			System.exit(-1);
		}
		
		
		
//		assert(rootNamespaceURI != null);
//		assert(!rootNamespaceURI.isEmpty());
//		assert(rootNamespaceURI.equals(namespaceURI));
//		assert(docRoot != null);
		//assert(docRoot.getNodeName() == qualifiedName);
		
		//Node docRoot = doc.getFirstChild();

//		
//		NamedNodeMap attributes = docRoot.getAttributes();
//		assert(attributes.getLength() <= 0); 
		
//		Iterator<Entry<String, String>> it = docRootAttributeMap.entrySet().iterator();
//	    while (it.hasNext()) {
//	        Map.Entry<String, String> pairs = (Map.Entry<String, String>)it.next();
//	        System.out.println(pairs.getKey() + " = " + pairs.getValue());
//	        String name = pairs.getKey();
//	        assert(name != null);
//	        assert(!name.isEmpty());
//	        
//	        String value = pairs.getValue();
//	        assert(value != null);
//	        assert(!value.isEmpty());
//	        
//	        System.out.println("Setting attribute on doc element, name: "+name+", value: "+value);
//	        
//	        ((Element)doc.getFirstChild()).setAttribute(name, value);
//	    }
		
		//return doc;

	}
	
	private static void printInfo(final DOMHelper domHelper) throws Exception{
		
		assert(domHelper.docElement() != null);
		System.out.println("Namespace: "+domHelper.namespaceUri());
		System.out.println("Root namespace: "+domHelper.docElement().getNamespaceURI());
		System.out.println("Root prefix: "+domHelper.docElement().getPrefix());
		
		assert(domHelper.docElement().getChildNodes().getLength() > 0);
		NodeList nodeListRO2 = DOMHelper.getElementListNS(domHelper.namespaceContext(), domHelper.docElement(), "//registryObject");
		NodeList nodeListROs2 = DOMHelper.getElementListNS(domHelper.namespaceContext(), domHelper.docElement(), "//registryObjects");
			
		NodeList nodeListRO = DOMHelper.getElementListNS(domHelper.namespaceContext(), domHelper.docElement(), "//ro:registryObject");
		NodeList nodeListROs = DOMHelper.getElementListNS(domHelper.namespaceContext(), domHelper.docElement(), "//ro:registryObjects");
		
		DOMHelper.printChildNodeInfo(domHelper.docElement());
	}

}
